package com.xbongbong.paas.util;

import com.xbongbong.paas.constant.ExcelConstant;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.StylesTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 * 解析07Excel用的xml hanlder
 * @author 魏荣杰
 * @date 2019/11/20 11:04 下午
 * @since v4.6.0
 * @version v4.6.0
 */
public class ExcelXlsxSheetXMLHandler extends XSSFSheetXMLHandler {
    private static final Logger LOG = LoggerFactory.getLogger(ExcelXlsxSheetXMLHandler.class);

    private ExcelXMLProcedure excelXMLProcedure;

    public ExcelXlsxSheetXMLHandler(StylesTable styles, CommentsTable comments, ReadOnlySharedStringsTable strings, SheetContentsHandler sheetContentsHandler, DataFormatter dataFormatter, boolean formulasNotResults) {
        super(styles, comments, strings, sheetContentsHandler, dataFormatter, formulasNotResults);
    }

    public ExcelXlsxSheetXMLHandler(StylesTable styles, ReadOnlySharedStringsTable strings, SheetContentsHandler sheetContentsHandler, DataFormatter dataFormatter, boolean formulasNotResults) {
        super(styles, strings, sheetContentsHandler, dataFormatter, formulasNotResults);
    }

    public ExcelXlsxSheetXMLHandler(StylesTable styles, ReadOnlySharedStringsTable strings, SheetContentsHandler sheetContentsHandler, boolean formulasNotResults) {
        super(styles, strings, sheetContentsHandler, formulasNotResults);
    }

    public ExcelXlsxSheetXMLHandler(StylesTable styles, CommentsTable comments, ReadOnlySharedStringsTable strings, SheetContentsHandler sheetContentsHandler, DataFormatter dataFormatter, boolean formulasNotResults, ExcelXMLProcedure excelXMLProcedure) {
        super(styles, comments, strings, sheetContentsHandler, dataFormatter, formulasNotResults);
        this.excelXMLProcedure = excelXMLProcedure;
    }

    public ExcelXlsxSheetXMLHandler(StylesTable styles, ReadOnlySharedStringsTable strings, SheetContentsHandler sheetContentsHandler, DataFormatter dataFormatter, boolean formulasNotResults, ExcelXMLProcedure excelXMLProcedure) {
        super(styles, strings, sheetContentsHandler, dataFormatter, formulasNotResults);
        this.excelXMLProcedure = excelXMLProcedure;
    }

    public ExcelXlsxSheetXMLHandler(StylesTable styles, ReadOnlySharedStringsTable strings, SheetContentsHandler sheetContentsHandler, boolean formulasNotResults, ExcelXMLProcedure excelXMLProcedure) {
        super(styles, strings, sheetContentsHandler, formulasNotResults);
        this.excelXMLProcedure = excelXMLProcedure;
    }

    @Override
    public void endDocument() throws SAXException {
        excelXMLProcedure.endDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (ExcelConstant.DIMENSION_ELEMENT.equals(qName)) {
            String ref = attributes.getValue(ExcelConstant.MERGE_CELL_REF_ATTR_VALUE);
            if (ref.contains(":")) {
                String totalStr = ref.substring(ref.indexOf(":") + 1, ref.length());
                String c = totalStr.toUpperCase().replaceAll("[A-Z]", "");
                excelXMLProcedure.setTotalRowNum(Integer.valueOf(c));
            } else {
                excelXMLProcedure.setTotalRowNum(1000);
            }
        }
        super.startElement(uri, localName, qName, attributes);
    }

    @Override
    public void warning(SAXParseException e) throws SAXException {
    }

    @Override
    public void error(SAXParseException e) throws SAXException {
    }

    @Override
    public void fatalError(SAXParseException e) throws SAXException {
//        super.fatalError(e);
        LOG.error("com.xbongbong.paas.util.ExcelXlsxSheetXMLHandler.fatalError error", e);
//        endDocument();
//        throw new SAXException("aaaaaa");
    }
}
